$(function () {

    $("#jqGrid").jqGrid({
        url: baseURL + 'goods/typeList',
        datatype: "json",
        colModel: [
            {label: '分类id', name: 'id', width: 30},
            {label: '分类名称', name: 'name', width: 20},
            {label: '打印机id', name: 'printerId', index: "printer_id", width: 20},
            {label: '打印机名称', name: 'printName', index: "printer_id", width: 20},
            {label: '商品数量', name: 'goodsNum',sortable : false, width: 40},
            {label: '创建时间', name: 'insertDate', index: "insert_date", width: 20},
        ],
        viewrecords: true,
        height: 720,
        rowNum: 19,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });

});

var vm = new Vue({
    el: '#dcapp',
    data: {
        selected: '',
        q: {
            name: null,
        },
        isAdd: false,
        showList: true,
        title: null,
        goodsType: {},
        options: [],
        formJson: null,
    },
    created: function () {

    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function () {
            vm.getPrinterList();
            var goodsTypeId = getSelectedRow();//选择一条
            if (goodsTypeId == null) {
                return;
            }
            vm.getGoodsType(goodsTypeId);
            vm.showList = false;
            vm.title = "编辑商品类型";
        },
        add: function () {
            vm.getPrinterList();
            vm.goodsType = {};
            vm.showList = false;
            vm.title = "增加商品类型";
        },
        saveOrUpdate: function () {
            var url = vm.goodsType.id == null?"goods/saveGoodsType":"goods/updateType";
            vm.formJson = JSON.stringify(vm.goodsType);
                $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: vm.formJson,
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },
        del: function () {
            var goodsTypeIds = getSelectedRows();
            if (goodsTypeIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "goods/delType",
                    contentType: "application/json",
                    data: JSON.stringify(goodsTypeIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function () {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getGoodsType: function (goodsTypeId) {
            $.ajax({
                type: "GET",
                url: baseURL + "goods/objType/" + goodsTypeId,
                async: false,
                success: function(r){
                    vm.goodsType = r.goodsType;
                }
            });

        },
        getPrinterList: function () {
            $.ajax({
                type: "GET",
                url: baseURL + "goods/printList",
                success: function(r){
                    vm.options = r.printList;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },

    }
});







